package ims.db.add.prodType;

import ims.beans.ProductType;
import ims.db.AbstractDAO;
import ims.db.AbstractDbRequest;
import ims.db.AbstractDbResponse;

import java.sql.SQLException;
import java.sql.Statement;

public class AddProdTypeDAO extends AbstractDAO {

	public AddProdTypeDAO() throws ClassNotFoundException, SQLException {
		super();
	}

	@Override
	public AbstractDbResponse execute(AbstractDbRequest dbRequest) 
	{
		AddProdTypeDbResponse response = new AddProdTypeDbResponse();
		AddProdTypeDbRequest request = (AddProdTypeDbRequest)dbRequest;
        try 
        {
        	ProductType prodType = request.getProdType();
        	Statement stmt = conn.createStatement();
        	StringBuffer insert = new StringBuffer();
        	insert.append("INSERT INTO ProductType (prodTypeId, productType) VALUES ('");
        	insert.append(prodType.getProdTypeId()).append("','").append(prodType.getProdType()).append("')");
        	
    		int retCd = stmt.executeUpdate(insert.toString());
    		if(retCd == 1)
    		{
    			response.setSuccess(true);
    			response.setMsg("Product type: "+prodType.getProdType()+" successfully added.");
    		}
    		else
    		{
    			response.setSuccess(false);
    			response.setMsg("Unable to add product type: "+prodType.getProdType());
    		}
            stmt.close();
            conn.close();
        } 
        catch (SQLException sqlEx)
        {
        	sqlEx.printStackTrace();
        }
        return response;
	}
}
